package com.ebt.m.customer.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ebt.m.R;
import com.ebt.m.customer.entity.Attachment;
import com.ebt.m.customer.entity.CompanyBean;
import com.ebt.m.customer.entity.PolicyBean;
import com.ebt.m.customer.entity.RiskBean;
import com.ebt.m.customer.ui.ImagePreviewActivity;
import com.ebt.m.customer.view.NestedGridView;
import com.ebt.m.customer.view.OptionDialog;
import com.ebt.m.customer.view.PairView;

import java.io.Serializable;
import java.text.NumberFormat;
import java.util.List;

/**
 * 客户保单
 * Created by archer.qi on 2016/12/12.
 */
public class CustomerPolicyAdapter extends BaseAdapter {

    private Context context;
    private List<PolicyBean> data;
    private LayoutInflater inflater;

    private PicAdapter mPicAdapter;
    private int positionOnClick; // 当前选中
    private OptionDialog moreDialog;

    private NumberFormat mNumberFormat;

    public CustomerPolicyAdapter(Context context, List<PolicyBean> data) {
        this.context = context;
        this.data = data;
        this.inflater = LayoutInflater.from(context);
        this.mNumberFormat = NumberFormat.getInstance();
    }

    public void update(List<PolicyBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public boolean isErp(int pos) {
        return data.get(pos).isErp();
    }

    public String getPolicyErpId(int pos) {
        return data == null ? "" : data.get(pos).getPolicyErpId();
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolderForAttachment holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_customer_active_policy, null);
            holder = new ViewHolderForAttachment(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolderForAttachment) view.getTag();
        }

        PolicyBean policy = data.get(i);

        if (policy != null) {
            holder.setErp(policy.isErp(), i);
            holder.setCompany(policy.getCompany());
            holder.setPolicy(policy);
            holder.setAttachments(policy.getAttachments());
        }

        return view;
    }

    private class ViewHolderForAttachment {
        // 品牌（公司）<通用>
        private ImageView mIvCompanyIcon;
        private TextView mTvCompanyName;
        private TextView mTvFlag;
        private ImageView mIvMore;

        // 通用客户
        private LinearLayout mLayoutCommon;
        private PairView mRisk;
        private PairView mPermium;
        private PairView mApplicantNo;
        private PairView mPolicyNo;
        private PairView mPayMode;
        private PairView mEffectDate;
        private NestedGridView mGridAttachments;

        // Erp
        private LinearLayout mLayoutErp;
        private PairView mErpRisk;
        private PairView mErpPolicyNo;
        private PairView mErpPermium;
        private PairView mErpPayMode;
        private PairView mErpEffectDate;
        private PairView mErpAcceptDate;
        private PairView mErpReceiptDate;
        private PairView mErpStatus;

        private View convertView;

        private boolean isErp;

        public ViewHolderForAttachment(View view) {
            if (view == null) {
                return;
            }
            convertView = view;

            mLayoutCommon = (LinearLayout) v(R.id.item_customer_policy_type_info);
            mLayoutErp = (LinearLayout) v(R.id.item_customer_policy_type_erp);

            mIvCompanyIcon = (ImageView) v(R.id.item_customer_policy_companyIcon);
            mTvCompanyName = (TextView) v(R.id.item_customer_policy_companyName);
            mTvFlag = (TextView) v(R.id.item_customer_policy_flag);
            mIvMore = (ImageView) v(R.id.item_customer_policy_opt);

            mRisk = (PairView) v(R.id.item_customer_policy_risk);
            mPolicyNo = (PairView) v(R.id.item_customer_policy_policyNo);
            mPermium = (PairView) v(R.id.item_customer_policy_premium);
            mApplicantNo = (PairView) v(R.id.item_customer_policy_applicantNo);
            mPayMode = (PairView) v(R.id.item_customer_policy_payMode);
            mEffectDate = (PairView) v(R.id.item_customer_policy_effectTime);
            mGridAttachments = (NestedGridView) v(R.id.item_customer_policy_attachments);

            mErpRisk = (PairView) v(R.id.item_customer_policy_erp_risk);
            mErpPolicyNo = (PairView) v(R.id.item_customer_policy_erp_policyNo);
            mErpPermium = (PairView) v(R.id.item_customer_policy_erp_permium);
            mErpPayMode = (PairView) v(R.id.item_customer_policy_erp_payMode);
            mErpEffectDate = (PairView) v(R.id.item_customer_policy_erp_effectDate);
            mErpAcceptDate = (PairView) v(R.id.item_customer_policy_erp_acceptDate);
            mErpReceiptDate = (PairView) v(R.id.item_customer_policy_erp_receiptDate);
            mErpStatus = (PairView) v(R.id.item_customer_policy_erp_policyStatus);
        }

        private View v(int id) {
            return convertView.findViewById(id);
        }

        public void setErp(boolean erp, int pos) {
            isErp = erp;
            mLayoutCommon.setVisibility(erp ? View.GONE : View.VISIBLE);
            mLayoutErp.setVisibility(erp ? View.VISIBLE : View.GONE);

            if (!isErp) {
                mIvMore.setImageResource(R.drawable.selector_item_more);
                mIvMore.setClickable(true);
                mTvFlag.setText("自建保单");
                mTvFlag.setTextColor(Color.parseColor("#37ac68"));
                mTvFlag.setBackgroundResource(R.drawable.bg_green_rect_corner);
                mIvMore.setOnClickListener(new onMoreClickListener(pos));
                mGridAttachments.setVisibility(View.VISIBLE);
            } else {
                mTvFlag.setText("ERP保单");
                mTvFlag.setTextColor(Color.parseColor("#ffab07"));
                mTvFlag.setBackgroundResource(R.drawable.bg_yellow_rect_corner);
                mIvMore.setImageResource(R.drawable.ic_arrow_right_gray);
                mIvMore.setClickable(false);
            }
            mGridAttachments.setVisibility(View.GONE);
        }

        /**
         * 公司
         *
         * @param company
         */
        public void setCompany(CompanyBean company) {
            if (company == null) {
                mIvCompanyIcon.setImageResource(R.drawable.ic_company_default);
                return;
            }

            String companyName = company.getShortName();
            if (!TextUtils.isEmpty(companyName)) {
                mTvCompanyName.setText(companyName);
            } else {
                mTvCompanyName.setText("");
            }

            String companyLogoUrl = company.getLogoUrl();
            if (!TextUtils.isEmpty(companyLogoUrl)) {
                Glide.with(context).load(companyLogoUrl).placeholder(R.drawable.ic_company_default).into(mIvCompanyIcon);
            }
        }

        /**
         * 通用基本信息
         *
         * @param policy
         */
        private void setPolicy(PolicyBean policy) {
            if (policy == null) {
                return;
            }

            RiskBean riskBean = policy.getRisk();
            String riskName = null;
            if (riskBean != null) {
                riskName = riskBean.getRiskName();
            }

            String policyNo = policy.getPolicyNo();
            String permiumValue = mNumberFormat.format(policy.getPermium()).replace(",", "");
            String applicantNo = policy.getInsureNo();
            String payMode = policy.getPayMode();
            String effectDate = policy.getEffectDate();
            String acceptDate = policy.getAcceptDate();
            String receiptDate = policy.getReceiptDate();
            String status = policy.getStatus();

            if (!isErp) {
                mRisk.setValueText(riskName);
                mPermium.setValueText(permiumValue);
                mApplicantNo.setValueText(applicantNo);
                mPolicyNo.setValueText(policyNo);
                mPayMode.setValueText(payMode);
                mEffectDate.setValueText(effectDate);
            } else {
                mErpRisk.setValueText(riskName);
                mErpPermium.setValueText(permiumValue);
                mErpPolicyNo.setValueText(policyNo);
                mErpPayMode.setValueText(payMode);
                mErpEffectDate.setValueText(effectDate);
                mErpAcceptDate.setValueText(acceptDate);
                mErpReceiptDate.setValueText(receiptDate);
                mErpStatus.setValueText(status);
            }
        }

        /**
         * 设置附件集显示
         *
         * @param attachments
         */
        private void setAttachments(List<Attachment> attachments) {
            if (isErp) {
                return;
            }
            if (attachments == null || (attachments != null && attachments.size() == 0)) {
                mGridAttachments.setVisibility(View.GONE);
            } else {
                mGridAttachments.setVisibility(View.VISIBLE);
            }
            mPicAdapter = new PicAdapter(context, attachments, false);
            mGridAttachments.setAdapter(mPicAdapter);
            mGridAttachments.setOnItemClickListener(new onPictureClickListener(attachments));
        }

        /**
         * 显示更多弹窗
         */
        private void showMore() {
            if (moreDialog == null) {
                moreDialog = new OptionDialog(context);
            }
            moreDialog.setData(data.get(positionOnClick));
            moreDialog.show(mIvMore);
        }

        private class onPictureClickListener implements AdapterView.OnItemClickListener {

            public List<Attachment> data;

            public onPictureClickListener(List<Attachment> data) {
                this.data = data;
            }

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i < 0 || i >= data.size()) {
                    return;
                }
                Intent intent = new Intent(context, ImagePreviewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(ImagePreviewActivity.EXTRA_DATA, (Serializable) data);
                bundle.putInt(ImagePreviewActivity.EXTRA_POSITION, i);
                bundle.putInt(ImagePreviewActivity.EXTRA_FLAG, 0);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        }

        private class onMoreClickListener implements View.OnClickListener {
            private int pos;

            public onMoreClickListener(int pos) {
                this.pos = pos;
            }

            @Override
            public void onClick(View view) {
                positionOnClick = pos;
                showMore();
            }
        }

    }
}
